﻿Imports Microsoft.VisualBasic
Imports System.ComponentModel
Imports System.Data
<System.ComponentModel.DataObject()> _
Public Class Departamentos
    Inherits BDDatos
    Sub New()
        Me.Tabla = "Departamentos"
        Me.Vista = "DEPARTAMENTOS"
    End Sub
    <DataObjectMethodAttribute(DataObjectMethodType.Select, True)> _
    Public Overloads Function GetRecords(ByVal busc As String) As DataTable
        Me.Conectar()
        If busc = "" Then
            querystring = "SELECT * FROM VDEPARTAMENTO"
            Me.CrearComando(querystring)
        Else
            querystring = "SELECT * FROM VDEPARTAMENTO where Cod_Depar Like :Buscar or Nom_Depar Like :Buscar"
            Me.CrearComando(querystring)
            AsignarParametroCadena(":Buscar", "%" & busc & "%")
            AsignarParametroCadena(":Buscar", "%" & busc & "%")
        End If
        Dim dataTb As DataTable = Me.EjecutarConsultaDataTable()
        Me.Desconectar()
        Return dataTb

    End Function
    Public Function Insert(ByVal idD As String, ByVal nomD As String) As String
        Me.Conectar()
        Me.ComenzarTransaccion()
        Try
            Me.querystring = "Insert into Departamento (Cod_Depar,Nom_Depar) Values(:Cod_Depar,:Nom_Depar)"
            Me.CrearComando(querystring)

            Me.AsignarParametroCadena(":Cod_Depar", idD)
            Me.AsignarParametroCadena(":Nom_Depar", nomD)
            Me.num_reg = EjecutarComando()
            Me.Msg = "Se realizo la Operacion Exitosamente - " + num_reg.ToString + " Filas Afectadas"
            Me.lErrorG = False

            Me.ConfirmarTransaccion()
        Catch ex As Exception
            CancelarTransaccion()
            Me.lErrorG = True
            Me.Msg = "Err: " + ex.Message
        Finally
            Me.Desconectar()
        End Try
        Return Msg
    End Function
    Public Function Update(ByVal PK As String, ByVal idD As String, ByVal nomD As String) As String
        Me.Conectar()
        Me.ComenzarTransaccion()
        Try
            Me.querystring = "Update Departamento set Cod_Depar=:Cod_Depar, Nom_Depar=:Nom_Depar  Where Cod_Depar=:PK"
            Me.CrearComando(querystring)
            Me.AsignarParametroCadena(":Cod_Depar", idD)
            Me.AsignarParametroCadena(":Nom_Depar", nomD)
            Me.AsignarParametroCadena(":PK", PK)
            Me.num_reg = EjecutarComando()
            Me.Msg = "Se realizo la Operacion Exitosamente - " + num_reg.ToString + " Filas Afectadas"
            Me.lErrorG = False
            Me.ConfirmarTransaccion()
        Catch ex As Exception
            CancelarTransaccion()
            Me.lErrorG = True
            Me.Msg = "Err: " + ex.Message
        Finally
            Me.Desconectar()
        End Try
        Return Msg
    End Function
    Public Function GetByPK(ByVal idD As String) As DataTable
        Me.Conectar()
        Me.querystring = "Select * from Departamento where Cod_Depar=:Cod_Depar"
        CrearComando(querystring)
        Me.AsignarParametroCadena(":Cod_Depar", idD)
        Dim dt As DataTable = EjecutarConsultaDataTable()
        Me.Desconectar()
        Return dt
    End Function
    Public Function Delete(ByVal idD As String) As String
        Me.Conectar()
        ComenzarTransaccion()
        Try
            Me.querystring = "Delete from Departamento where Cod_Depar=:Cod_Depar"
            CrearComando(querystring)
            Me.AsignarParametroCadena(":Cod_Depar", idD)
            EjecutarComando()
            Msg = MsgOk
            ConfirmarTransaccion()
        Catch ex As Exception
            CancelarTransaccion()
            Msg = ex.Message
        Finally
            Me.Desconectar()
        End Try
        Return Msg
    End Function


End Class
